package cj.web.admin.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.admin.dao.SurveyGroupDAO;
import cj.web.admin.dao.SurveyPeopleLoginDAO;
import cj.web.admin.domain.SurveyGroup;
import cj.web.admin.domain.SurveyPeopleLogin;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

@Path("/surveyPeopleLogin")
public class SurveyPeopleLoginController extends BaseController{

	private static Logger logger = Logger.getLogger(SurveyPeopleLoginController.class.getName());
	
	@Autowired
	private SurveyPeopleLoginDAO surveyPeopleLoginDao;
	@Autowired
	private SurveyGroupDAO surveyGroupDAO;
	
	@Get("show")
	public String show(Model model) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgksrygl);
		return "/views/survey/surveyPeopleLoginList.vm";
	}

	@Post("getList")
	public String queryList(SurveyPeopleLogin surveyPeopleLogin) {
		JSONObject jo = new JSONObject();
		List<SurveyPeopleLogin> arr = surveyPeopleLoginDao.query(surveyPeopleLogin);
		
		int size = surveyPeopleLoginDao.querysize(surveyPeopleLogin);
		
		jo.put("data", arr);
		jo.put("draw", surveyPeopleLogin.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@" + jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model){
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgksrygl);
        List<SurveyGroup> surveylis = surveyGroupDAO.queryAll();
        model.add("surveylis",surveylis);
		return "/views/survey/surveyPeopleLoginDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,SurveyPeopleLogin surveyPeopleLogin,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			surveyPeopleLogin.setDeleteFlag(1);
			surveyPeopleLogin.setCreateUser(returnSessionObj(request).getAccount());
			surveyPeopleLogin.setUpdateUser(returnSessionObj(request).getAccount());
			surveyPeopleLoginDao.create(surveyPeopleLogin);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id){
		SurveyPeopleLogin surveyPeopleLogin = surveyPeopleLoginDao.queryById(id);
        //MemuEnum
        model.add("MemuEnum",MemuEnum.lkgksrygl);
        model.add("obj",surveyPeopleLogin);
        List<SurveyGroup> surveylis = surveyGroupDAO.queryAll();
        model.add("surveylis",surveylis);
		return "/views/survey/surveyPeopleLoginEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,SurveyPeopleLogin surveyPeopleLogin){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			surveyPeopleLoginDao.update(surveyPeopleLogin);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id){
		surveyPeopleLoginDao.deleteById(id);
		return "r:show";
	}
}